<?php
require_once( "include/incMain.php" );


$message = "";
$show = "";

$action = $_REQUEST['action']; 
$show = $_REQUEST['show']; 
$id = $_REQUEST['id']; 


if($show == "") $show = "add";



function getLoginID($CharID) {
    $sql = "SELECT login_id FROM characters WHERE id = '$CharID' ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $retVal = $row['login_id'];
    }
    return $retVal;
}
function getSelectLogin($CharID) {
    $retVal = "";
    $array_id = "";
    $array_name = "";
    $tmpArray[0] = "NPC";
    $sql = "SELECT id, username FROM login ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $tmpArray[$row['id']] = $row['username'];
    }
    $retVal[] = array( "options" => $tmpArray, "selected" => getLoginID($CharID) );
    return $retVal;
}
function getController($login_id) {
    if($login_id==0) return "NPC";
    $retVal = "NO";
    $sql = "SELECT username FROM login WHERE id = '$login_id' ";
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $retVal = $row['username'];
    }
    return $retVal;
}
function getContent($id) {
    $retVal = "";
    if($id=="")  $where = " 1 ";
    else $where = " characters.id = '$id' ";
    
    $sql = "SELECT characters.id, characters.name, characters.login_id, characters.prestige, COUNT(objects.id) AS obj_sum FROM characters LEFT JOIN objects ON objects.character_id = characters.id WHERE $where GROUP BY characters.id  ";
 
    $result = sql($sql);
    while($row = mysql_fetch_array( $result )) {
        $retVal[] = array( "id" => $row['id'], 
                           "name" => $row['name'],
                           "controller" => getController($row['login_id']),
                           "prestige" => $row['prestige'],
                           "obj_sum" => $row['obj_sum']);
    }
    return $retVal;
}
function addContent() {
    $name = $_REQUEST['charactersName'];
    $prestige = $_REQUEST['charactersPrestige'];
    $login_id = $_REQUEST['characterController'];
    
     
    $sql = "INSERT INTO characters (name, prestige, login_id) VALUES ('$name', '$prestige', '$login_id') ";
    sql($sql);
    return "ADDED";
}
function saveContent() {
    $id = $_REQUEST['id']; 
    $name = $_REQUEST['charactersName'];
    $prestige = $_REQUEST['charactersPrestige'];
     
    $sql = "UPDATE characters SET name = '$name', prestige = '$prestige' WHERE id = '$id' ";
    sql($sql);
    return "SAVED";
}
function deleteContent() {
    $id = $_REQUEST['id'];
    sql("DELETE FROM characters WHERE id = '$id' ");
    sql("DELETE FROM objects WHERE character_id = '$id' ");
}



if($show == "add") $message = "ADD";
if($show == "edit") $message = "EDIT";
if($show == "delete") $message = "DELETE";

if($action == "add") $message = addContent();
if($action == "save") $message = saveContent();
if($action == "delete") $message = deleteContent();
if($action == "back" || $action == "delete") {
    $id = "";
    $show = "add";
}





$smarty = new mySmarty();
$smarty->assign("Path", glPath );
$smarty->assign("id", $id );
$smarty->assign("action", $action );
$smarty->assign("show", $show );
$smarty->assign("message", $message );
$smarty->assign("menue", getMenue(3) );
$smarty->assign("ContentSelect", getSelectLogin($id) );
$smarty->assign("Content", getContent($id) );
$smarty->display(glPath.'/characters.htm');


?>
